package cn.seaboot.commons.lang;

import cn.seaboot.commons.core.CommonUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 标准树节点
 * <p>
 * 对于树这种数据结构，我们希望以统一的方式进行处理，
 * <p>
 * 要求所有数据转为 {@link TreeNode}，方便后续代码的封装。
 *
 * @author Mr.css
 * @version 2024-04-26 10:50
 */
public class TreeNode implements Serializable {
    private static final long serialVersionUID = -8373046449086958894L;
    /**
     * ID 关键字
     */
    private String id;
    /**
     * 父级 ID 关键字
     */
    private String pid;
    /**
     * 标签
     */
    private String label;
    /**
     * 图标
     */
    private String icon;
    /**
     * 是否选中
     */
    private Boolean checked;
    /**
     * 目标树：节点值
     */
    private Number value;
    /**
     * 目标树：节点得分
     */
    private Number score;
    /**
     * 数据实体
     */
    private Object data;
    /**
     * 父节点
     * <p>
     * 严格上来说，一个节点也可能属于多个节点，
     * 不过目前这个字段，仅用于查找根节点，不影响业务，暂不考虑优化
     */
    private transient TreeNode parent;
    /**
     * 子节点
     */
    private List<TreeNode> children;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    /**
     * 压制泛型警告，需要程序员自己注意泛型
     *
     * @param <T> 指定泛型
     * @return 原始数据
     */
    @SuppressWarnings(Warning.UNCHECKED)
    public <T> T getData() {
        return (T) data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Boolean getChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    public Number getValue() {
        return value;
    }

    public void setValue(Number value) {
        this.value = value;
    }

    public Number getScore() {
        return score;
    }

    public void setScore(Number score) {
        this.score = score;
    }

    /**
     * 是否有父级
     *
     * @return if true
     */
    public boolean hasParent() {
        return this.parent != null;
    }

    /**
     * 是否有子节点
     *
     * @return if true
     */
    public boolean hasChildren() {
        return CommonUtils.isNotEmpty(children);
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "id='" + id + '\'' +
                ", pid='" + pid + '\'' +
                ", label='" + label + '\'' +
                ", icon='" + icon + '\'' +
                ", checked='" + checked + '\'' +
                ", data=" + data +
                ", children=" + children +
                '}';
    }
}
